package com.arunsawad.ilertu.activity;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.Validator;

public class Languages extends Activity {
	SharedPreferences preferences;
	String[] languages;
	String[] languageKeys;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.act_languages);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title0_imgbtn_txt);
		
		initTitle();
		
		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);
		Validator.Validate(this, preferences);
		
		// get languages source - to keys and values
		languages = getResources().getStringArray(R.array.languages);
		String[] languageValues = new String[languages.length];
		languageKeys = new String[languages.length];
		for (int i = 0; i < languages.length; i++) {
			languageValues[i] = languages[i].split("-")[0];
			languageKeys[i] = languages[i].split("-")[1];
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice, languageValues);
		ListView listView = (ListView) findViewById(R.id.languages_lv);
		listView.setAdapter(adapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		// set current language
		String language = preferences.getString(Constants.PREF_LANGUAGE, "en");
		int index = 0;
		for (int i = 0; i < languageKeys.length; i++) {
			if (languageKeys[i].equals(language)) {
				index = i;
				break;
			}
		}
		listView.setItemChecked(index, true);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ListView lv = (ListView) arg0;
				lv.setItemChecked(arg2, true);
				updateLanguage(arg2);
			}
		});
	}

	public void initTitle() {
		ImageButton imgBtn1 = (ImageButton) findViewById(R.id.title0_imgBtn1);
		imgBtn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Home.class);
				startActivity(intent);
			}
		});

		TextView txtTitle = (TextView) findViewById(R.id.title0_txtTitle);
		txtTitle.setText(getString(R.string.language));
	}
	
	public void updateLanguage(int index) {
		String language = languageKeys[index];
		Locale locale = new Locale(language);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config,
				getBaseContext().getResources().getDisplayMetrics());

		SharedPreferences.Editor editor = preferences.edit();
		editor.putString(Constants.PREF_LANGUAGE, language);
		editor.commit();

		Intent myIntent = new Intent(this, Settings.class);
		startActivity(myIntent);
	}
}